package com.vvlive.caibao.smartcity.base;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.vvlive.caibao.smartcity.utils.ScreenUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.KJActivity;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/6/30.
 * BaseActivity, based on KJActivity.
 * <p/>
 * About CommonActionBar{@link CommonActionBar}
 * <p/>
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |            method                            |                           details                               |
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |            toast(Object msg);                |Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show(); |
 * +----------------------------------------------+-----------------------------------------------------------------|
 * |            alert(Object msg);                |                有一个确定按钮的AlertDialog                        |
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |  confirm(Object msg, OnConfirmListener l);   |                       有两个按钮的AlertDialog                     |
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |            log(Object msg);                  |                  Log.i("SmartCity", msg.toString());            |
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |    log(String tag, Object msg);              |                    Log.i(tag, msg.toString());                  |
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |  setCommonActionBarFloating(boolean floating)|                    设置CommonActionBar是否浮动                    |
 * +----------------------------------------------+-----------------------------------------------------------------+
 * |  setCommonActionBarVisible(boolean visible)  |                    设置CommonActionBar是否可见                    |
 * +----------------------------------------------+-----------------------------------------------------------------+
 */
public abstract class BaseActivity extends KJActivity {

    /**
     * Log tag
     */
    private final String TAG = "SmartCity";

    /**
     * Confirm callback
     */
    private ConfirmClickListener confirmClickListener;

    /**
     * Custom actionbar{@link CommonActionBar}, with three parts, back button, title, more button
     */
    private CommonActionBar mCommonActionBar;

    /**
     * A Layout contains the view you wanna display
     */
    private RelativeLayout contentContainer;

    /**
     * The view based on the content resource you offer
     */
    private View contentView;

    /**
     * Toast
     */
    private Toast mToast;

    private Intent intent;

    private int MARGIN_TOP = 0;

    /**
     * This abstract method has to be implemented in subclasses.
     *
     * @return The content view resource
     */
    protected abstract int getContentViewRes();

    @Override
    public void setRootView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_common);
        MARGIN_TOP = (int) new ScreenUtil(this).dip2px(CommonActionBar.ACTIONBAR_HEIGHT);
        setContent();
    }

    private void setContent() {
        contentContainer = (RelativeLayout) this.findViewById(R.id.content);
        mCommonActionBar = (CommonActionBar) this.findViewById(R.id.common_actionbar);
        contentView = getLayoutInflater().inflate(getContentViewRes(), null, false);
        contentView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        // set the marginLeft for content view.
        setContentContainerMarginTop(MARGIN_TOP);
        // add the content to layout.
        contentContainer.addView(contentView);
    }

    /**
     * Get the action bar.{@link CommonActionBar}
     *
     * @return The actionbar.
     */
    public CommonActionBar getCommonActionBar() {
        return this.mCommonActionBar;
    }

    /**
     * Set if the actionbar floating, floating default.
     *
     * @param isFloating Floating if it is true, not floating otherwise.
     */
    public void setCommonActionBarFloating(boolean isFloating) {
        setContentContainerMarginTop(isFloating ? 0 : MARGIN_TOP);
    }

    // Inner method, you won't get it.^-^
    private void setContentContainerMarginTop(int marginTop) {
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) contentContainer.getLayoutParams();
        marginLayoutParams.setMargins(0, marginTop, 0, 0);
        contentContainer.requestLayout();
    }

    /**
     * Set if the actionbar visible.
     *
     * @param visible Visible if it is true, gone otherwise.
     */
    public void setCommonActionBarVisible(boolean visible) {
        int visibility = visible ? View.VISIBLE : View.GONE;
        int marginTop = visible ? MARGIN_TOP : 0;
        mCommonActionBar.setVisibility(visibility);
        setContentContainerMarginTop(marginTop);
    }

    /**
     * AlertDialog with 2 buttons, positive and negative.
     *
     * @param message           The message you wanna confirm
     * @param onConfirmListener The callback function which returns which button you click.
     * @return AlertDialog
     */
    protected AlertDialog confirm(Object message, OnConfirmListener onConfirmListener) {
        if (confirmClickListener == null) {
            confirmClickListener = new ConfirmClickListener(onConfirmListener);
        }
        AlertDialog alert = new AlertDialog.Builder(this).setMessage(message.toString()).setPositiveButton("确定", confirmClickListener)
                .setPositiveButton("取消", confirmClickListener).create();
        alert.show();
        return alert;
    }

    private final class ConfirmClickListener implements OnClickListener {

        private OnConfirmListener confirmListener;

        public ConfirmClickListener(OnConfirmListener onConfirmListener) {
            this.confirmListener = onConfirmListener;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_NEGATIVE:
                    if (confirmListener != null)
                        confirmListener.onConfirm(false);
                    break;
                case DialogInterface.BUTTON_POSITIVE:
                    if (confirmListener != null)
                        confirmListener.onConfirm(true);
                    break;
            }
        }
    }

    /**
     * Confirm callback function class.
     */
    public interface OnConfirmListener {
        void onConfirm(boolean isConfirmed);
    }

    /**
     * <strong>AlertDialog with one button.</strong><br>
     * The difference between the method "alert" and "toast" is
     * that "alert" would get focus automatically, and won't
     * disappear until you click the button.
     *
     * @param message The message you wanna show.
     * @return AlertDialog
     */
    protected AlertDialog alert(Object message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message.toString());
        builder.setPositiveButton("确定", null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        return alertDialog;
    }

    /**
     * As "Toast.makeText(context, text, duration).show()"
     *
     * @param text The message you wanna show.
     * @return Toast
     */
    protected Toast toast(Object text) {
        if (mToast == null) {
            mToast = Toast.makeText(this, text.toString(), Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text.toString());
        }
        mToast.show();
        return mToast;
    }

    /**
     * start activity
     *
     * @param cls
     */
    protected void newActivity(Class cls) {
        newActivity(cls, null);
    }

    protected void newActivity(Class cls, Bundle extras) {
        startNewActivity(cls, extras);
    }

    private void startNewActivity(Class c, Bundle e) {
        if (intent == null) {
            intent = new Intent();
        }
        if (e != null) {
            intent.putExtras(e);
        }
        intent.setClass(this, c);
        startActivity(intent);
    }

    /**
     * Log something with the default tag "SmartCity".
     *
     * @param msg The message you wanna log.
     */
    protected void log(Object msg) {
        log(TAG, msg);
    }

    /**
     * Log something with the tag you provide.
     *
     * @param tag tag
     * @param msg message
     */
    protected void log(String tag, Object msg) {
        Log.i(tag, msg.toString());
    }

}
